package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.ClassinfoQueryObject;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Autowired
    private IClassinfoService classinfoService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IRoleService roleService;

    //查询全部方法
    @RequestMapping("/list")
    public String list(Model model , ClassinfoQueryObject qo){

        List<Employee> employees = employeeService.selectAll();
        model.addAttribute("employees",employees);

        PageInfo pageResult = classinfoService.query(qo);
        //List<Department> departments = departmentService.selectAll();
        model.addAttribute("pageResult",pageResult);
        System.out.println(pageResult);
        //model.addAttribute("departments",departments);
        model.addAttribute("qo",qo);
        return "classinfo/list";
    }

    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                classinfoService.deleteByPrimaryKey(id);
            }
            return new JsonResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败");
        }

    }

    // 新增 或 修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo) {
        System.out.println(classinfo);
        try {
            if (classinfo.getId() != null) {
                //修改
                classinfoService.updateByPrimaryKey(classinfo);
            } else {
                classinfoService.insert(classinfo);
            }
            return new JsonResult(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(false,"失败");
    }




}
